package com.kingdee.pda.plugins.http.serverinterface.impl;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.kingdee.pda.entity.ReplenishCommitBean;
import com.kingdee.pda.entity.ReplenishOrderDetailBean;
import com.example.basecommon.utils.LoginUtil;
import com.kingdee.pda.plugins.http.common.KeyConstants;
import com.kingdee.pda.plugins.http.common.OnRequestCallback;
import com.kingdee.pda.util.CommonUtil;
import com.example.basecommon.utils.ConstantUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * 补货单详情查询
 * Created by dlj
 * DATE 2021/6/25
 * TIME 14:39
 */
public class QueryReplenishOrderDetailImpl {

    private static final String API_NAME = "/kapi/app/oewms/query_app_pda_replenishbill_detail";

    public void queryReplenishOrderDetail(long warehouseId, String billNo, final OnRequestCallback<ReplenishOrderDetailBean> callback) {
        String apiUrl = ConstantUtil.getServerUrl() + API_NAME;
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"),
                setListParamValue(warehouseId, billNo).toString());

        Request request = new Request.Builder()
                .addHeader("accessToken", LoginUtil.getToken())
                .url(apiUrl)
                .post(requestBody)
                .build();
        OkHttpUtils.getInstance().getOkHttpClient().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                callback.onFailedCallback(false, CommonUtil.getErrorMsg(e.getMessage(), "请求失败！"));
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.code() == 200) {
                    String result = response.body().string();
                    if (!TextUtils.isEmpty(result) && result.contains(KeyConstants.Response.SUCCESS)) {
                        try {
                            ReplenishOrderDetailBean replenishOrderBean = new Gson().fromJson(result, ReplenishOrderDetailBean.class);
                            callback.onSuccessCallback(replenishOrderBean, "");
                        } catch (Exception e) {
                            e.printStackTrace();
                            callback.onFailedCallback(false, CommonUtil.getErrorMsg(response.message(), "数据解析失败！"));
                        }
                    } else {
                        callback.onFailedCallback(true, CommonUtil.getErrorMsg(response.message(), "请求失败！"));
                    }
                } else {
                    callback.onFailedCallback(false, response.message());
                }
            }
        });
    }

    private Object setListParamValue(long warehouseId, String billNo) {
        JSONObject json = new JSONObject();
        try {
            json.put("warehouseId", warehouseId);
            json.put("billNo", billNo);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return json;
    }
}
